<template>
  <div class='main'>
    <a-form
      id='formLogin'
      class='user-layout-login'
      ref='formLogin'
      :form='form'
      @submit='handleSubmit'
    >
      <a-tabs
        :activeKey='customActiveKey'
        :tabBarStyle="{ textAlign: 'center', borderBottom: 'unset' }"
        @change='handleTabClick'
      >
        <!--账号密码登录-->
        <a-tab-pane key='tab1' :tab="'候选人登录'">
          <a-alert v-if='isLoginError'
                   type='error'
                   showIcon
                   style='margin-bottom: 24px;'
                   :message='errMsg' />
          <a-form-item>
            <a-input
              size='large'
              type='text'
              :placeholder="'请输入账号'"
              v-decorator="[
                'username',
                {rules: [{ required: true, message: '账号为不能为空!' }], validateTrigger: 'blur'}
              ]"
            >
              <a-icon
                slot='prefix'
                type='user'
                :style="{ color: 'rgba(0,0,0,.25)' }" />
            </a-input>
          </a-form-item>
          
          <a-form-item>
            <a-input-password
              size='large'
              :placeholder="'请输入密码'"
              v-decorator="[
                'password',
                {rules: [{ required: true, message: '密码不能为空!' }], validateTrigger: 'blur'}
              ]"
            >
              <a-icon
                slot='prefix'
                type='lock'
                :style="{ color: 'rgba(0,0,0,.25)' }" />
            </a-input-password>
          </a-form-item>
        </a-tab-pane>
        
        <!--管理员登录(并没有作用,用的还是同一个接口,纯属装逼)-->
        <a-tab-pane key='tab2' :tab="'管理员登录'">
          <a-alert
            v-if='isLoginError'
            type='error' showIcon
            style='margin-bottom: 24px;'
            :message='errMsg' />
          <a-form-item>
            <a-input
              size='large'
              type='text'
              :placeholder="'请输入管理员账号'"
              v-decorator="[
                'username',
                {rules: [{ required: true, message: '账号为不能为空' }], validateTrigger: 'blur'}
              ]"
            >
              <a-icon
                slot='prefix'
                type='user'
                :style="{ color: 'rgba(0,0,0,.25)' }" />
            </a-input>
          </a-form-item>
          
          <a-form-item>
            <a-input-password
              size='large'
              :placeholder="'管理员密码'"
              v-decorator="[
                'password',
                {rules: [{ required: true, message: '密码不能为空' }], validateTrigger: 'blur'}
              ]"
            >
              <a-icon
                slot='prefix'
                type='lock'
                :style="{ color: 'rgba(0,0,0,.25)' }" />
            </a-input-password>
          </a-form-item>
        
        </a-tab-pane>
      
      
      </a-tabs>
      
      <a-form-item>
        <a-checkbox v-decorator="['rememberMe', { valuePropName: 'checked' }]">自动登录</a-checkbox>
        <router-link
          @click='forgetPwd'
          class='forge-password'
          style='float: right;'>
          忘记密码
        </router-link>
      </a-form-item>
      
      <a-form-item style='margin-top:24px'>
        <a-button
          size='large'
          type='primary'
          htmlType='submit'
          class='login-button'
          :loading='state.loginBtn'
          :disabled='state.loginBtn'
        >登录
        </a-button>
      </a-form-item>
      
      <!--底部的三个图标-->
      <div class='user-login-other'>
        
        <router-link class='register' :to="{ name: 'register' }">注册账号</router-link>
      </div>
    </a-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { timeFix } from '@/utils/util'

export default {
  data() {
    return {
      customActiveKey: 'tab1',
      loginBtn: false,
      errMsg: '',
      isLoginError: false,
      requiredTwoStepCaptcha: false,
      form: this.$form.createForm(this),
      state: {
        time: 60,
        loginBtn: false
      }
    }
  },
  created() {
  },
  methods: {
    ...mapActions(['Login', 'Logout']),
    // 标签切换
    handleTabClick(key) {
      this.customActiveKey = key
      // 清空数据
      this.form.resetFields()
    },
    // 提交表单
    handleSubmit(e) {
      e.preventDefault()
      const {
        form: { validateFields },
        state,
        customActiveKey,
        Login
      } = this
      
      state.loginBtn = true
      
      const validateFieldsKey = customActiveKey === 'tab1' ? ['username', 'password'] : ['username', 'password']
      // 表单校验
      validateFields(validateFieldsKey, { force: true }, (err, values) => {
        if (!err) {
          const loginParams = { ...values }
          delete loginParams.username
          loginParams.username = values.username
          loginParams.password = values.password
          Login(loginParams)
            .then((res) => {
              if (res.code === 200) {
                this.loginSuccess()
              } else {
                this.loginFailed(res)
              }
              // 账号密码错误/账号被锁定/账号封禁
            })
            .finally(() => {
              state.loginBtn = false
            })
        } else {
          setTimeout(() => {
            state.loginBtn = false
          }, 600)
        }
      })
    },
    
    loginSuccess() {
      // 登录成功后进行跳转
      this.$router.push({ path: '/account/center' })
      // 延迟 1 秒显示欢迎信息
      setTimeout(() => {
        this.$notification.success({
          message: '欢迎',
          description: `${timeFix()}，欢迎回来`
        })
      }, 1000)
      this.isLoginError = false
    },
    loginFailed(res) {
      this.isLoginError = true
      this.errMsg = res.msg
    },
    forgetPwd(){
      this.$message.error("嘿嘿 不允许忘记密码哦!")
    }
  }
}
</script>

<style lang='less' scoped>
.user-layout-login {
  label {
    font-size: 14px;
  }
  
  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }
  
  .forge-password {
    font-size: 14px;
  }
  
  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }
  
  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;
    
    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;
      
      &:hover {
        color: #1890ff;
      }
    }
    
    .register {
      float: right;
    }
  }
}
</style>
